package com.pwc.conversionTool.dao;

import static com.pwc.conversionTool.entity.service.OfyService.ofy;

import java.util.List;

import com.googlecode.objectify.Key;
import com.pwc.conversionTool.entity.Account;
import com.pwc.conversionTool.entity.CsvDataType;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.ExcelDataType;
import com.pwc.conversionTool.entity.Formula;

public class DataAdaptorDao {

	public Key<DataTemplate> saveDataAdaptor(DataTemplate dataAdaptor) {
		return ofy().save().entity(dataAdaptor).now();
	}

	public DataTemplate getDataAdaptorById(long id) {
		return ofy().load().type(DataTemplate.class).id(id).now();
	}

	public void deleteDataAdaptor(Long id) {
		ofy().delete().type(DataTemplate.class).id(id).now();
	}

	// public List<DataTemplate> getDataAdaptorsByEmail(String email)
	// {
	// return ofy().load().type(DataTemplate.class).filter("email =",
	// email).list();
	// }

	public List<DataTemplate> getDataAdaptorsByAccount(Account account) {
		return ofy().load().type(DataTemplate.class).filter("account", account).list();
	}

	public CsvDataType getCsvDataTypeByDataTemplate(DataTemplate dataTemplate) {
		CsvDataType res = ofy().load().type(CsvDataType.class).filter("dataTemplate", dataTemplate).first().now();

		return res;
	}

	public Key<CsvDataType> saveCsvDataType(CsvDataType csvDataType) {
		return ofy().save().entity(csvDataType).now();
	}

	public CsvDataType getCsvDataTypeById(Long id) {
		return ofy().load().type(CsvDataType.class).id(id).now();
	}

	public void deleteCsvDataType(Long Id) {
		ofy().delete().type(CsvDataType.class).id(Id).now();
	}

	public void deleteCsvDataType(CsvDataType csvDataType) {
		ofy().delete().entity(csvDataType).now();
	}

	public void deleteCsvDataType(DataTemplate dataTemplate) {
		CsvDataType csvDataType = getCsvDataTypeByDataTemplate(dataTemplate);
		
		if(csvDataType!=null)
		deleteCsvDataType(csvDataType);
	}

	public ExcelDataType getExcelDataType(DataTemplate oldDataAdaptor) {
		
		return ofy().load().type(ExcelDataType.class).filter("dataTemplate", oldDataAdaptor).first().now();
	}

	public void deleteExcelDataType(ExcelDataType oldExcelDataType) {
		ofy().delete().entity(oldExcelDataType).now();
		
	}

	public void saveExcelDataType(ExcelDataType oldExcelDataType) {
		
		ofy().save().entity(oldExcelDataType).now();
	}

	public void saveFormula(Formula formula) {
		
		ofy().save().entity(formula).now();
		
	}

	public Formula getFormulaByDataTemplate(Key<DataTemplate> key) {		
		
		return ofy().load().type(Formula.class).filter("dataTemplateKey", key).first().now();
	}



}
